import {
  behavior as computedBehavior
} from "miniprogram-computed";
import {
  isCellEmpty,
  FormatDateTime,
  getDeviceConfig,
  cutStringValue
} from "../../../../utils/util"
import {
  EVENT_BTN_CLICK
} from "../../track"

Component({
  behaviors: [computedBehavior],
  /**
   * 组件的属性列表
   */
  properties: {
    powers: {
      type: String,
      value: ""
    },
    userInfo: {
      type: Object,
      value: ""
    },
    scheduleList: {
      type: Array,
      value: []
    },
    teamList: {
      type: Array,
      value: []
    },
    //之前的人写的一个日历组件，选一个日期居然要整两个参数，日了狗了~~~
    riliList: {
      type: Array,
      value: []
    },
    addTime: {
      type: String,
      value: ""
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    tabList: [{
      name: "日程管理",
      value: "日程管理"
    }],
    tabValue: "日程管理",

    scheduleScrollTop: 0,
  },

  computed: {
    isLogin(data) {
      const {
        userInfo
      } = data;

      let result = false;
      if (userInfo && !isCellEmpty(userInfo.id)) {
        result = true
      }

      return result;
    },
    hasAdd(data) {
      const powers = data.powers;

      return powers.indexOf("12-202") >= 0;
    },
    scrollHeight(data) {
      const { isLogin } = data;

      const deviceConfig = getDeviceConfig();

      const {
        windowScale,
        windowHeight
      } = deviceConfig;

      let height;

      //修正4个像素点，防止整屏抖动
      const fixedHeight = 0;
      if(isLogin) {
        height = 428 - 98 + 252 + 104 + fixedHeight;
      } else {
        height = 428 - 160 + 272 + 104 + fixedHeight;
      }

      return windowHeight * windowScale - height;
    },
    researcherIds(data) {
      const teamList = data.teamList;
      const result = [];

      teamList.forEach(item => {
        if (item.checked && item.fUserId != 0) {
          result.push(item.fUserId)
        }
      })

      return result;
    },
    researcherNames(data) {
      const teamList = data.teamList;
      const result = [];

      teamList.forEach(item => {
        if (item.checked && item.fUserId != 0) {
          result.push(item.fUserName)
        }
      })

      return result;
    },
    researcherNamesShow(data) {
      const { researcherNames } = data;

      return cutStringValue(researcherNames.join(","), 20, "...");
    }
  },

  watch: {
    powers: function (v) {
      let result = [{
        name: "日程管理",
        value: "日程管理"
      }];
      if (v.indexOf('12-207') >= 0) { //团队日程显示
        result.push({
          name: "团队日程",
          value: "团队日程"
        })
      }
      if (v.indexOf('12-209') >= 0) {
        result.push({
          name: "研究员日程",
          value: "研究员日程"
        })
      }

      this.setData({
        tabList: result
      })
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handleClickTab(e) {
      const value = e.currentTarget.dataset.value;

      this.setData({
        tabValue: value
      }, () => {

        if (value == '日程管理') {
          this.handleTabChange("日程管理");
        } else if (value == '团队日程') {
          this.handleTabChange("团队日程");
        } else if (value === '研究员日程') {
          this.goToResearcherRicheng();
        }
      })
    },
    /**
     * @前往研究员日程
     */
    goToResearcherRicheng() {
      EVENT_BTN_CLICK("日程tab", "研究员日程");

      let dateTime = ''
      if (this.data.addTime) {
        dateTime = this.data.addTime
      } else {
        dateTime = FormatDateTime(new Date(), "yyyy-MM-dd");
      }

      wx.navigateTo({
        url: `/pages/reservation/list/index?dateTime=${dateTime}`
      })
    },

    /**
     * @tab切换
     */
    handleTabChange(type) {
      EVENT_BTN_CLICK("日程tab", type);

      this.triggerEvent("onTab", type);
    },

    handleTipTab(e) {
      const value = e.currentTarget.dataset.value;

      this.triggerEvent("onTipTab", value)
    },

    /**
     * @点击添加我的日程
     */
    handleAddSchedule() {
      this.triggerEvent("onAdd");
    },

    /**
     * @点击日程前往页面
     * @param {*} e 
     */
    handleClickScheduleItem(e) {
      const item = e.detail; 
      this.triggerEvent("onSchedule", item);
    },

    /**
     * @点击团队日程
     */
    handleClickTeam() {
      this.triggerEvent("onTeam");
    }
  }
})